package com.hzit.servlet;

import com.alibaba.fastjson.JSON;
import com.hzit.entity.Classes;
import com.hzit.entity.R;
import com.hzit.service.ClassesService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/classes")
public class ClassesServlet extends HttpServlet {
	private ClassesService classesService;
	@Override
	public void init() throws ServletException {
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		classesService = wac.getBean(ClassesService.class);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf8");
		//1. 查询出所有班级
		List<Classes> classesList = classesService.findAll();
		//2. 将班级列表放到r对象中
		R<List<Classes>> r = R.ok(classesList);
		//3. 将r对象转换为字符串
		String s = JSON.toJSONString(r);
		//4. 输出s
		resp.getWriter().println(s);
		resp.getWriter().flush();
	}
}
